package com.star.wellconnect.profile;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.star.wellconnect.R;
import com.star.wellconnect.sqlite.AppPreferences;
import com.star.wellconnect.sqlite.Patient;

public class HomeFragment extends Fragment{
	private Patient mPatient;
	private ImageView mProfilePic;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment_home, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState){
		super.onViewCreated(view, savedInstanceState);
		
		mPatient = AppPreferences.getPatient(getActivity());
		
		mProfilePic = (ImageView) view.findViewById(R.id.profile_pic);
		mProfilePic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ProfileActivity.class);
                startActivity(intent);
			}
		});
		
		if(mPatient.image != null){
			mProfilePic.setImageBitmap(BitmapFactory.decodeByteArray(mPatient.image, 0, mPatient.image.length));
		}
		
		((TextView) view.findViewById(R.id.welcome))
		.setText("Welcome" + (mPatient.name != null && !mPatient.name.isEmpty()? " " + mPatient.name+"!":"!"));
		
		view.findViewById(R.id.my_profile_button)
		.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ProfileActivity.class);
                startActivity(intent);
			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
}